using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class tbchitietdonhangDAL
    {
         private SqlConnection sqlCn = null;
        static public string error = "";
        public tbchitietdonhangDAL()
        {
            string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
            sqlCn = new SqlConnection(stringConnection);
            sqlCn.Open();
        }
        ~tbchitietdonhangDAL()
        {
            //this.sqlCn.Close();
            //this.sqlCn.Dispose();
        }

        public void Insert(tbchitietdonhangDTO ctdh)
        {
            SqlCommand cm = new SqlCommand("sp_chitietdonhang_Insert", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madh", ctdh.Madh);
            cm.Parameters.AddWithValue("@masp", ctdh.Masp);
            cm.Parameters.AddWithValue("@soluong", ctdh.Soluong);
            cm.Parameters.AddWithValue("@dongia", ctdh.Dongia);
            try
            {
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return;
            }
        }

        public void Update(tbchitietdonhangDTO ctdh)
        {
            SqlCommand cm = new SqlCommand("sp_chitietdonhang_Update", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mactdh", ctdh.Mactdh);
            cm.Parameters.AddWithValue("@madh", ctdh.Madh);
            cm.Parameters.AddWithValue("@masp", ctdh.Masp);
            cm.Parameters.AddWithValue("@soluong", ctdh.Soluong);
            cm.Parameters.AddWithValue("@dongia", ctdh.Dongia);
            try
            {
                cm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return;
            }
        }

        public void Delete(int Ma)
        {
            SqlCommand cm = new SqlCommand("sp_chitietdonhang_Delete", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@mactdh", Ma);

            cm.ExecuteNonQuery();
        }

        public tbchitietdonhangDTOColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                       string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_chitietdonhang_GET", this.sqlCn);
            cm.CommandType = CommandType.StoredProcedure;


            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            tbchitietdonhangDTOColection tbchitietdonhangDTOColection = new tbchitietdonhangDTOColection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                tbchitietdonhangDTO ctdh = new tbchitietdonhangDTO();
                ctdh.Mactdh= int.Parse(ds.Tables[0].Rows[i]["mactdh"].ToString());
                ctdh.Madh = int.Parse(ds.Tables[0].Rows[i]["madh"].ToString());
                ctdh.Masp = int.Parse(ds.Tables[0].Rows[i]["masp"].ToString());
                ctdh.Soluong = int.Parse(ds.Tables[0].Rows[i]["soluong"].ToString());
                ctdh.Dongia = float.Parse(ds.Tables[0].Rows[i]["dongia"].ToString());
                tbchitietdonhangDTOColection.Add(ctdh);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());

            return tbchitietdonhangDTOColection;
        }
    }
}
